﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.BussinessDelegate.DanhMucChung;

using QLPKWeb.Models.FormBean.DanhMucChungModel;
using QLPKWeb.Models.LinQ.DanhMucChung;
using QLPKWeb.Controllers.Base;

using dm_LoaiNgoaiTe = QLPKWeb.Models.LinQ.DanhMucChung.dm_LoaiNgoaiTe;
using dw_InformationCompany = QLPKWeb.Models.LinQ.DanhMucChung.dw_InformationCompany;

namespace QLPKWeb.Controllers
{
    public class DanhMucChungController : BaseController
    {
#region phần xử lý cho DANH MỤC Đơn vị

        DonViDelegate DonViDelegate = new DonViDelegate();

        //....phần xử lý cho DANH MỤC Đơn vị .............................................

        public ActionResult DonVi(int? id)// id : page number
        {
            DonViForm form = new DonViForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới DonVi;

            form.DonViPagedList = DonViDelegate.GetDonViPagedList(id);

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/DonVi.aspx", form);
        }

        //Create DonVi
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TaoDonVi(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/DonVi/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult TaoDonVi(DonViForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_LoaiDVTinh newDonVi = this.DonViDelegate.BuildNewDonVi(form);
            try
            {
                newDonVi = this.DonViDelegate.AddNewDonVi(newDonVi);
                form.MessageCreateDonVi = "Tạo mới thành công !";
            }
            catch (Exception)
            {
                form.MessageCreateDonVi = "Tạo mới thất bại !";
            }

            //Refesh form
            form.Id = newDonVi.Id;
            form.DonViPagedList = DonViDelegate.GetDonViPagedList(form.PageNumber);


            return View("~/Views/QuanLyDanhMucChung/DonVi.aspx", form);
        }

        //Edit DonVi
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChinhSuaDonVi(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/DonVi/" + id);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ChinhSuaDonVi(DonViForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_LoaiDVTinh editDonVi = this.DonViDelegate.BuildEditDonVi(form);

            try
            {
                this.DonViDelegate.UpdateDonVi(editDonVi);
                form.MessageEditDonVi = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.MessageEditDonVi = "Cập nhật thất bại !";
            }

            //Refesh form
            form.DonViPagedList = DonViDelegate.GetDonViPagedList(form.PageNumber);

            return View("~/Views/QuanLyDanhMucChung/DonVi.aspx", form);
        }

        //Xóa danh mục Đơn vị
        public ActionResult DeleteDonVi(string DonViID)
        {
            this.DonViDelegate.DeleteDonVi(DonViID);
            return RedirectToAction("DonVi");
        }

        //Search Danh mục Đơn vị
        public ActionResult TimKiemDonVi(int? id)// id : page number
        {
            DonViForm form = new DonViForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới DonVi;

            string searchCriteria = Request["DonViSearch"];

            form.DonViPagedList = DonViDelegate.SearchDonViPagedList(id, searchCriteria);// id : page number

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/DonVi.aspx", form);
        }

        //para ten : Tên nơi lưu trữ
        public JsonResult DoCheckTenDonViExistAjax(string TenDonVi)
        {
            dm_LoaiDVTinh item = this.DonViDelegate.GetDonViByTen(TenDonVi);

            bool result = item != null ? true : false;

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DonViEditAjax(string id)
        {
            dm_LoaiDVTinh item = this.DonViDelegate.GetDonViById(id);

            string[] kq = new string[] { item.Id.ToString(), item.Ten };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }

        //....end DANH MỤC Đơn vị .........................................................
        #endregion
        #region phần xử lý cho DANH MỤC NgoaiTe

        NgoaiTeDelegate NgoaiTeDelegate = new NgoaiTeDelegate();

        //....phần xử lý cho DANH MỤC Đơn vị .............................................

        public ActionResult NgoaiTe(int? id)// id : page number
        {
            NgoaiTeForm form = new NgoaiTeForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới NgoaiTe;

            form.NgoaiTePagedList = NgoaiTeDelegate.GetNgoaiTePagedList(id);

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/NgoaiTe.aspx", form);
        }

        //Create NgoaiTe
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TaoNgoaiTe(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/NgoaiTe/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult TaoNgoaiTe(NgoaiTeForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_LoaiNgoaiTe newNgoaiTe = this.NgoaiTeDelegate.BuildNewNgoaiTe(form);
            try
            {
                newNgoaiTe = this.NgoaiTeDelegate.AddNewNgoaiTe(newNgoaiTe);
                form.MessageCreateNgoaiTe = "Tạo mới thành công !";
            }
            catch (Exception)
            {
                form.MessageCreateNgoaiTe = "Tạo mới thất bại !";
            }

            //Refesh form
            form.Id = newNgoaiTe.Id;
            form.NgoaiTePagedList = NgoaiTeDelegate.GetNgoaiTePagedList(form.PageNumber);


            return View("~/Views/QuanLyDanhMucChung/NgoaiTe.aspx", form);
        }

        //Edit NgoaiTe
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChinhSuaNgoaiTe(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/NgoaiTe/" + id);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ChinhSuaNgoaiTe(NgoaiTeForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_LoaiNgoaiTe editNgoaiTe = this.NgoaiTeDelegate.BuildEditNgoaiTe(form);

            try
            {
                this.NgoaiTeDelegate.UpdateNgoaiTe(editNgoaiTe);
                form.MessageEditNgoaiTe = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.MessageEditNgoaiTe = "Cập nhật thất bại !";
            }

            //Refesh form
            form.NgoaiTePagedList = NgoaiTeDelegate.GetNgoaiTePagedList(form.PageNumber);

            return View("~/Views/QuanLyDanhMucChung/NgoaiTe.aspx", form);
        }

        //Xóa danh mục Đơn vị
        public ActionResult DeleteNgoaiTe(string NgoaiTeID)
        {
            this.NgoaiTeDelegate.DeleteNgoaiTe(NgoaiTeID);
            return RedirectToAction("NgoaiTe");
        }

        //Search Danh mục Đơn vị
        public ActionResult TimKiemNgoaiTe(int? id)// id : page number
        {
            NgoaiTeForm form = new NgoaiTeForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới NgoaiTe;

            string searchCriteria = Request["NgoaiTeSearch"];

            form.NgoaiTePagedList = NgoaiTeDelegate.SearchNgoaiTePagedList(id, searchCriteria);// id : page number

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/NgoaiTe.aspx", form);
        }

        //para ten : Tên nơi lưu trữ
        public JsonResult DoCheckTenNgoaiTeExistAjax(string TenNgoaiTe)
        {
            dm_LoaiNgoaiTe item = this.NgoaiTeDelegate.GetNgoaiTeByTen(TenNgoaiTe);

            bool result = item != null ? true : false;

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult NgoaiTeEditAjax(string id)
        {
            dm_LoaiNgoaiTe item = this.NgoaiTeDelegate.GetNgoaiTeById(id);

            string[] kq = new string[] { item.Id.ToString(), item.Ten };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }
        #region NamnvlMoney
        public JsonResult NgoaiTeMonneyListByIdNameAjax(int id)
        {
            NgoaiTeMonneyDelegate NgoaiTeMonneyDelegate = new NgoaiTeMonneyDelegate();
            string str = "";
            str += "<input type='hidden' id='IdNgoaiTe' name='IdNgoaiTe' value='" + id + "'/>";
            str += "<table  class='data display datatable'>";
            str += "<tr style='font-weight: bold'>" +
                         
                          "<td  style='border: 1px solid #ddd ' >Rate</td>" +
                          "</tr>";
            foreach (var o in NgoaiTeMonneyDelegate.GetNgoaiTeMonneyByIdNgoaiTe(id))
            {
                str += " <tr class='item_row' onclick='javascript:highlight(this);EditNgoaiTeMonneyAjax(" + o.Id + ");'";
                if (o.IsActive == true)
                {
                    str += "style='color: blue'";
                }
                str += " > " +
                 
                           "<td  style='border: 1px solid #ddd '>" + o.GiaTriTiGia + "</td>" +
                           "<td  style='border: 1px solid #ddd '><a onclick=javascript:ActiveAjax(" + o.Id + ");>Active</a></td>" +
                      " </tr>";
            }
            str += "</table>";
            return Json(str, JsonRequestBehavior.AllowGet);

        }
        public JsonResult SaveNgoaiTeMonney(string Monney, int IdNgoaiTe)
        {
   
            dm_NgoaiTe_TiGia dw = new dm_NgoaiTe_TiGia();
            dw.GiaTriTiGia = Monney;
           
            dw.IdNgoaiTe = IdNgoaiTe;
            NgoaiTeMonneyDelegate NgoaiTeMonneyDelegate = new NgoaiTeMonneyDelegate();
            NgoaiTeMonneyDelegate.AddNewNgoaiTe(dw);
            string str = "";
            str += "<input type='hidden' id='IdNgoaiTe' name='IdNgoaiTe' value='" + IdNgoaiTe + "'/>";
            str += "<table  class='data display datatable'>";
            str += "<tr style='font-weight: bold'>" +
                         
                          "<td  style='border: 1px solid #ddd ' >Rate</td>" +
                          "</tr>";
            foreach (var o in NgoaiTeMonneyDelegate.GetNgoaiTeMonneyByIdNgoaiTe(IdNgoaiTe))
            {
                str += " <tr class='item_row' onclick='javascript:highlight(this);EditNgoaiTeMonneyAjax(" + o.Id + ");'";
                if (o.IsActive == true)
                {
                    str += "style='color:blue'";
                }
                str += " > " +
                           "<td  style='border: 1px solid #ddd '>" + o.GiaTriTiGia + "</td>" +
                           "<td  style='border: 1px solid #ddd '><a onclick=javascript:ActiveAjax(" + o.Id + ");>Active</a></td>" +
                      " </tr>";
            }
            str += "</table>";
            return Json(str, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SaveUpdateNgoaiTeMonney(int Id, string Monney,  int IdNgoaiTe)
        {
         
            dm_NgoaiTe_TiGia dw = new dm_NgoaiTe_TiGia();
            dw.Id = Id;
            dw.GiaTriTiGia = Monney;
          
            //   dw.IdNamNVL = IdNgoaiTe;
            NgoaiTeMonneyDelegate NgoaiTeMonneyDelegate = new NgoaiTeMonneyDelegate();
            NgoaiTeMonneyDelegate.UpdateNgoaiTe(dw);
            string str = "";
            str += "<input type='hidden' id='IdNgoaiTe' name='IdNgoaiTe' value='" + IdNgoaiTe + "'/>";
            str += "<table  class='data display datatable'>";
            str += "<tr  style='font-weight: bold'>" +
                          "<td  style='border: 1px solid #ddd ' >Rate</td>" +
                          "</tr>";
            foreach (var o in NgoaiTeMonneyDelegate.GetNgoaiTeMonneyByIdNgoaiTe(IdNgoaiTe))
            {
                str += " <tr class='item_row' onclick='javascript:highlight(this);EditNgoaiTeMonneyAjax(" + o.Id + ");'";
                if (o.IsActive == true)
                {
                    str += "style='color:blue'";
                }
                str += " > " +
                           "<td  style='border: 1px solid #ddd '>" + o.GiaTriTiGia + "</td>" +
                           "<td  style='border: 1px solid #ddd '><a onclick=javascript:ActiveAjax(" + o.Id + ");>Active</a></td>" +
                      " </tr>";
            }
            str += "</table>";
            return Json(str, JsonRequestBehavior.AllowGet);
        }
        public JsonResult NgoaiTeMonneyEditAjax(string id)
        {
            NgoaiTeMonneyDelegate NgoaiTeMonneyDelegate = new NgoaiTeMonneyDelegate();
            dm_NgoaiTe_TiGia item = NgoaiTeMonneyDelegate.GetNgoaiTeById(id);
            string[] kq = new string[] { item.Id.ToString(), item.GiaTriTiGia };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Active(int id)
        {
            NgoaiTeMonneyDelegate NgoaiTeMonneyDelegate = new NgoaiTeMonneyDelegate();
            dm_NgoaiTe_TiGia item = NgoaiTeMonneyDelegate.GetNgoaiTeById(id.ToString());
            NgoaiTeMonneyDelegate.Active(id);
            string str = "";
            str += "<input type='hidden' id='IdNgoaiTe' name='IdNgoaiTe' value='" + item.IdNgoaiTe + "'/>";
            str += "<table  class='data display datatable'>";
            str += "<tr  style='font-weight: bold'>" +
                         
                          "<td  style='border: 1px solid #ddd ' >Date</td>" +
                          "</tr>";
            foreach (var o in NgoaiTeMonneyDelegate.GetNgoaiTeMonneyByIdNgoaiTe(item.IdNgoaiTe.Value))
            {
                str += " <tr class='item_row' onclick='javascript:highlight(this);EditNgoaiTeMonneyAjax(" + o.Id + ");'";
                if (o.IsActive == true)
                {
                    str += "style='color:blue'";
                }
                str += " > " +
                           "<td  style='border: 1px solid #ddd '>" + o.GiaTriTiGia + "</td>" +
                           "<td  style='border: 1px solid #ddd '><a onclick=javascript:ActiveAjax(" + o.Id + ");>Active</a></td>" +
                      " </tr>";
            }
            str += "</table>";
            return Json(str, JsonRequestBehavior.AllowGet);
        }

        #endregion

        //....end DANH MỤC Đơn vị .........................................................
        #endregion
#region phần xử lý cho DANH MỤC Đối tác

        NCCDelegate NCCDelegate = new NCCDelegate();

        //....phần xử lý cho DANH MỤC Đối tác .............................................

        public ActionResult NCC(int? id)// id : page number
        {
            NCCForm form = new NCCForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới NCC;

            form.NCCPagedList = NCCDelegate.GetNCCPagedList(id);

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/NCC.aspx", form);
        }

        //Create NCC
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TaoNCC(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/NCC/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult TaoNCC(NCCForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_NhaCC newNCC = this.NCCDelegate.BuildNewNCC(form);
            try
            {
                newNCC = this.NCCDelegate.AddNewNCC(newNCC);
                form.MessageCreateNCC = "Tạo mới thành công !";
            }
            catch (Exception)
            {
                form.MessageCreateNCC = "Tạo mới thất bại !";
            }

            //Refesh form
            form.Id = newNCC.Id;
            form.NCCPagedList = NCCDelegate.GetNCCPagedList(form.PageNumber);


            return View("~/Views/QuanLyDanhMucChung/NCC.aspx", form);
        }

        //Edit NCC
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChinhSuaNCC(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/NCC/" + id);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ChinhSuaNCC(NCCForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_NhaCC editNCC = this.NCCDelegate.BuildEditNCC(form);

            try
            {
                this.NCCDelegate.UpdateNCC(editNCC);
                form.MessageEditNCC = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.MessageEditNCC = "Cập nhật thất bại !";
            }

            //Refesh form
            form.NCCPagedList = NCCDelegate.GetNCCPagedList(form.PageNumber);

            return View("~/Views/QuanLyDanhMucChung/NCC.aspx", form);
        }

        //Xóa danh mục Đối tác
        public ActionResult DeleteNCC(string NCCID)
        {
            this.NCCDelegate.DeleteNCC(NCCID);
            return RedirectToAction("NCC");
        }

        //Search Danh mục Đối tác
        public ActionResult TimKiemNCC(int? id)// id : page number
        {
            NCCForm form = new NCCForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới NCC;

            string searchCriteria = Request["NCCSearch"];

            form.NCCPagedList = NCCDelegate.SearchNCCPagedList(id, searchCriteria);// id : page number

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/NCC.aspx", form);
        }

        //para ten : Tên nơi lưu trữ
        public JsonResult DoCheckTenNCCExistAjax(string ten)
        {
            dm_NhaCC item = this.NCCDelegate.GetNCCByTen(ten);

            bool result = item != null ? true : false;

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult NCCEditAjax(string id)
        {
            dm_NhaCC item = this.NCCDelegate.GetNCCById(id);

            string[] kq = new string[] { item.Id.ToString(), item.TenNCC,item.DiaChi,item.SoDT,item.Fax,item.Mail,item.Website,item.MaST,item.NganHang,item.TaiKhoan };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }

        //....end DANH MỤC Đối tác .....................................................
        public string chuyenchu(string a)
        {
            var b = a.Replace("Š", "Ị").Replace("Š", "Ị").Replace("”", "Ô").Replace("†", "Ệ").Replace("‡", "ệ").Replace("Cng", "Công")
                .Replace("•", "Ổ").Replace("’", "Ồ").Replace("#", "Ể").Replace("€", "Ề").Replace("Ị", "Ê").Replace("“", "ồ")
                .Replace("™", "Ộ").Replace("Ề", "À").Replace("‹", "ị").Replace("thnh", "thành").Replace("‰", "ỉ").Replace("ƒ", "ă")
                .Replace("Tn bnh", "Tân Bình").Replace("‘", "đ").Replace("‘", "ố").Replace("Bnh", "Bình").Replace("…", "ễ").Replace("—i", "ị")
                .Replace("Ể", "Â").Replace("gim", "Giám").Replace("ˆ", "Ỉ").Replace("˜", "Ộ").Replace("›", "ớ").Replace("tn", "Tân")
                .Replace("uyn", "Uyên").Replace("ž", "Ở").Replace("Ê", "Ị")
                ;
            return b;
        }


        public ActionResult ReadAndSaveExcel(IEnumerable<HttpPostedFileBase> UploadControl)
        {
            string connectionString = "";
            if (UploadControl.Count() > 0)
            {
                foreach (var file1 in UploadControl)
                {
                    var fileName = Path.GetFileName(file1.FileName);
                    string directoryPath = Server.MapPath("~/ReportTemp/");
                    var destinationPath = Path.Combine(directoryPath, fileName);
                    file1.SaveAs(destinationPath);
                    string fileLocation = Server.MapPath("~/ReportTemp/" + fileName);
                    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation +
                                       ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    OleDbConnection con = new OleDbConnection(connectionString);
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Connection = con;
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                    DataTable dtExcelRecords = new DataTable();
                    con.Open();
                    DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                    cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                    dAdapter.SelectCommand = cmd;
                    dAdapter.Fill(dtExcelRecords);
                    dtExcelRecords.TableName = "Excel";
                    dm_NhaCC ncc = new dm_NhaCC();
                    for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
                    {
                        ncc = new dm_NhaCC();
                        ncc.TenNCC = chuyenchu(dtExcelRecords.Rows[i]["Partner Name"].ToString());
                        ncc.IdMaNCC = dtExcelRecords.Rows[i]["PartnerID"].ToString();
                        ncc.LocalName =  chuyenchu(dtExcelRecords.Rows[i]["Local Name"].ToString());
                        ncc.ForeignName =  chuyenchu(dtExcelRecords.Rows[i]["Foreign name"].ToString());
                        ncc.DiaChi =  chuyenchu(dtExcelRecords.Rows[i]["Address"].ToString());
                        ncc.SoDT = dtExcelRecords.Rows[i]["Phone"].ToString();
                        ncc.Fax = dtExcelRecords.Rows[i]["Fax"].ToString();
                        ncc.Website = dtExcelRecords.Rows[i]["Website"].ToString();
                        ncc.MaST = dtExcelRecords.Rows[i]["Taxcode"].ToString();
                        var ID = dtExcelRecords.Rows[i]["PartnerID"].ToString().Trim();
                        if (ID != "")
                        {
                            if (ID[0] == 'F' || ID[0] == 'P')
                            {
                                ncc.IsKhuVuc = 1;
                            }
                            else
                            {
                                ncc.IsKhuVuc = 2;
                            }
                        }
                        else
                        {
                            ncc.IsKhuVuc = 2; 
                        }
                       
                        ncc.IsNVL = true;
                      
                        NCCDelegate.AddNewNCC(ncc);
                    }
                    con.Close();
                }
            }
            return null;
        }
        #endregion
        #region phần xử lý cho DANH MỤC Company

        CompanyDelegate CompanyDelegate = new CompanyDelegate();

        //....phần xử lý cho DANH MỤC Đối tác .............................................

        public ActionResult Company(int? id)// id : page number
        {
            CompanyForm form = new CompanyForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới Company;

            form.CompanyPagedList = CompanyDelegate.GetCompanyPagedList(id);

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/Company.aspx", form);
        }

        //Create Company
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TaoCompany(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/Company/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult TaoCompany(CompanyForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dw_InformationCompany newCompany = this.CompanyDelegate.BuildNewCompany(form);
            try
            {
                newCompany = this.CompanyDelegate.AddNewCompany(newCompany);
                form.MessageCreateCompany = "Tạo mới thành công !";
            }
            catch (Exception)
            {
                form.MessageCreateCompany = "Tạo mới thất bại !";
            }

            //Refesh form
            form.Id = newCompany.Id;
            form.CompanyPagedList = CompanyDelegate.GetCompanyPagedList(form.PageNumber);


            return View("~/Views/QuanLyDanhMucChung/Company.aspx", form);
        }

        //Edit Company
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChinhSuaCompany(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/Company/" + id);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ChinhSuaCompany(CompanyForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dw_InformationCompany editCompany = this.CompanyDelegate.BuildEditCompany(form);

            try
            {
                this.CompanyDelegate.UpdateCompany(editCompany);
                form.MessageEditCompany = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.MessageEditCompany = "Cập nhật thất bại !";
            }

            //Refesh form
            form.CompanyPagedList = CompanyDelegate.GetCompanyPagedList(form.PageNumber);

            return View("~/Views/QuanLyDanhMucChung/Company.aspx", form);
        }

        //Xóa danh mục Đối tác
        public ActionResult DeleteCompany(string CompanyID)
        {
            this.CompanyDelegate.DeleteCompany(CompanyID);
            return RedirectToAction("Company");
        }

        //Search Danh mục Đối tác
        public ActionResult TimKiemCompany(int? id)// id : page number
        {
            CompanyForm form = new CompanyForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới Company;

            string searchCriteria = Request["CompanySearch"];

            form.CompanyPagedList = CompanyDelegate.SearchCompanyPagedList(id, searchCriteria);// id : page number

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/Company.aspx", form);
        }

        //para ten : Tên nơi lưu trữ
        public JsonResult DoCheckTenCompanyExistAjax(string ten)
        {
            dw_InformationCompany item = this.CompanyDelegate.GetCompanyByTen(ten);

            bool result = item != null ? true : false;

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CompanyEditAjax(string id)
        {
            dw_InformationCompany item = this.CompanyDelegate.GetCompanyById(id);

            string[] kq = new string[] { item.Id.ToString(), item.CompanyName,item.SoTaiKhoan,item.MaSoThue, item.DiaChi, item.SoDT, item.Fax, item.Mail, item.Website };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }

        //....end DANH MỤC Đối tác .........................................................
        #endregion
        #region phần xử lý cho DANH MỤC Khách hàng

        KhachHangDelegate KhachHangDelegate = new KhachHangDelegate();

        //....phần xử lý cho DANH MỤC Đối tác .............................................

        public ActionResult KhachHang(int? id)// id : page number
        {
            KhachHangForm form = new KhachHangForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới KhachHang;

            form.KhachHangPagedList = KhachHangDelegate.GetKhachHangPagedList(id);
            form.SLLoaiKhachHang = KhachHangDelegate.BuilLoaiKhachHang(0);

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/KhachHang.aspx", form);
        }

        //Create KhachHang
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TaoKhachHang(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/KhachHang/" + id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult TaoKhachHang(KhachHangForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_KhachHang newKhachHang = this.KhachHangDelegate.BuildNewKhachHang(form);
            try
            {
                newKhachHang = this.KhachHangDelegate.AddNewKhachHang(newKhachHang);
                form.MessageCreateKhachHang = "Tạo mới thành công !";
            }
            catch (Exception)
            {
                form.MessageCreateKhachHang = "Tạo mới thất bại !";
            }

            //Refesh form
            form.Id = newKhachHang.Id;
            form.KhachHangPagedList = KhachHangDelegate.GetKhachHangPagedList(form.PageNumber);
            form.SLLoaiKhachHang = KhachHangDelegate.BuilLoaiKhachHang(newKhachHang.IdLoaiKhachHang);

            return View("~/Views/QuanLyDanhMucChung/KhachHang.aspx", form);
        }

        //Edit KhachHang
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChinhSuaKhachHang(int? id)// id : page number
        {
            return Redirect("~/DanhMucChung/KhachHang/" + id);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ChinhSuaKhachHang(KhachHangForm form)
        {
            //Lấy thông tin Job Title mới từ Form
            dm_KhachHang editKhachHang = this.KhachHangDelegate.BuildEditKhachHang(form);

            try
            {
                this.KhachHangDelegate.UpdateKhachHang(editKhachHang);
                form.MessageEditKhachHang = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.MessageEditKhachHang = "Cập nhật thất bại !";
            }

            //Refesh form
            form.KhachHangPagedList = KhachHangDelegate.GetKhachHangPagedList(form.PageNumber);
            form.SLLoaiKhachHang = KhachHangDelegate.BuilLoaiKhachHang(editKhachHang.IdLoaiKhachHang);
            return View("~/Views/QuanLyDanhMucChung/KhachHang.aspx", form);
        }

        //Xóa danh mục Đối tác
        public ActionResult DeleteKhachHang(string KhachHangID)
        {
            this.KhachHangDelegate.DeleteKhachHang(KhachHangID);
            return RedirectToAction("KhachHang");
        }

        //Search Danh mục Đối tác
        public ActionResult TimKiemKhachHang(int? id)// id : page number
        {
            KhachHangForm form = new KhachHangForm();
            form.Id = -1; //Mặc định _ dấu hiệu tạo mới KhachHang;

            string searchCriteria = Request["KhachHangSearch"];

            form.KhachHangPagedList = KhachHangDelegate.SearchKhachHangPagedList(id, searchCriteria);// id : page number

            form.PageNumber = id ?? 1;

            return View("~/Views/QuanLyDanhMucChung/KhachHang.aspx", form);
        }

        //para ten : Tên nơi lưu trữ
        public JsonResult DoCheckTenKhachHangExistAjax(string ten)
        {
            dm_KhachHang item = this.KhachHangDelegate.GetKhachHangByTen(ten);

            bool result = item != null ? true : false;

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult KhachHangEditAjax(string id)
        {
            dm_KhachHang item = this.KhachHangDelegate.GetKhachHangById(id);

            string[] kq = new string[] { item.Id.ToString(), item.TenKH, item.DiaChi, item.SoDT, item.Fax, item.Email, item.WebSite, item.MaST,item.IdLoaiKhachHang.ToString() };
            return Json(kq, JsonRequestBehavior.AllowGet);
            //return Json(item, JsonRequestBehavior.AllowGet);
        }
        //....end DANH MỤC Đối tác .....................................................
      
        #endregion

    }
}
